package com.seriializable;

import java.io.*;
import java.util.ArrayList;

public class StorageDog {


    /**
     * 出现了peek.byte的错误是由于多读了对象
     * @param args
     */
    public static void main(String[] args) {
        try{
            /**
             * 将集合写入文件
             */
            ArrayList<Integer> list = new ArrayList<>();
            list.add(23);
            list.add(11);
            FileOutputStream fos = new FileOutputStream("D://Dog.txt");
            ObjectOutputStream  os = new ObjectOutputStream(fos);
            os.writeObject(list);
            os.close();
            fos.close();
        }catch (IOException e){
            e.printStackTrace();
        }
        /**
         *反序列化集合
         */
        try
        {
            FileInputStream ios = new FileInputStream("D://Dog.txt");
            ObjectInputStream is = new ObjectInputStream(ios);
            ArrayList<Integer> list = (ArrayList)is.readObject();
            for (Integer e : list){
                System.out.println(e);
            }

        }catch (IOException | ClassNotFoundException e){
            e.printStackTrace();
        }


    }
}
